﻿Imports System.Data.SqlClient

Public Class admeditmember

    Inherits System.Web.UI.Page
    Private sSQL As String
    Private role As String
    Private objrd As SqlDataReader = Nothing
    Private objfind As New GetData
    Private ps As New PreparedStatement(sSQL)
    Private langFlag As String
    Dim strErr As String = ""

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim strMid, active, gender As String
        If (Session("admin_ID") <> Nothing And Session("admin_ID") <> "") Then
            strMid = Trim(Request("mid"))
            langFlag = Request("langFlag")
            ps.addParameter("id", strMid)
            If Request("btnUpdate") <> Nothing And Request("btnUpdate") = "Update" Then
                Exit Sub
            End If
            sSQL = "select * from member where GAPortal_ID=@id"
            ps.strSql = sSQL
            Dim conn As SqlConnection = objfind.getConn()

            objrd = objfind.dr(ps, strErr, conn, objrd)
            If objrd.Read() Then
                txtEmail.Text = objrd("email").ToString
                email.Text = txtEmail.Text
                gender = objrd("gender").ToString
                txtPhone.Text = objrd("phone").ToString
                txtCellphone.Text = objrd("cellphone").ToString
                active = objrd("active").ToString
                If active = 1 Then
                    ch.Checked = True
                Else
                    ch.Checked = False
                End If
                If gender = 0 Then
                    rMale.Checked = True
                Else
                    rFemale.Checked = True
                End If
            End If
            If langFlag = "zh" Then
                sSQL = "select * from member_info where member_id=@id and lang_flag='zh'"
            ElseIf langFlag = "en" Then
                sSQL = "select * from member_info where member_id=@id and lang_flag='en'"
            End If
            ps.strSql = sSQL
            objrd = objfind.dr(ps, strErr, conn, objrd)
            If objrd.Read() Then
                txtName.Text = objrd("member_name").ToString
                isHas.Text = txtName.Text
            End If
            objfind.closeConn(conn)

        Else
            Response.Redirect("../adm_login.aspx")
        End If
    End Sub
    Protected Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click

        If txtEmail.Text = "" Then
            result.InnerHtml = ""
            result.InnerHtml = "<center><b><font color=""#FF0000""> Pleas enter your email</font></b></center>"
            Exit Sub
        End If
        If txtName.Text = "" Then
            result.InnerHtml = ""
            result.InnerHtml = "<center><b><font color=""#FF0000""> Pleas enter your name</font></b></center>"
            Exit Sub
        End If
        If txtPhone.Text = "" Then
            result.InnerHtml = ""
            result.InnerHtml = "<center><b><font color=""#FF0000""> Pleas enter your contact phone</font></b></center>"
            Exit Sub
        End If
        ps.addParameter("email", txtEmail.Text)
        If email.Text <> txtEmail.Text Then
            sSQL = "select * from member where email=@email"

            ps.strSql = sSQL
            Dim conn As SqlConnection = objfind.getConn()

            objrd = objfind.dr(ps, strErr, conn, objrd)
            If objrd.Read() Then
                result.InnerHtml = ""
                result.InnerHtml = "<center><b><font color=""#FF0000""> The email is existed</font></b></center>"
                objfind.closeConn(conn)

                Exit Sub
            End If
            objfind.closeConn(conn)

        End If


        Dim act As String

        Dim gender As String
        If ch.Checked = True Then
            act = 1
        Else
            act = 0
        End If
        If rMale.Checked = True Then
            gender = 0
        Else
            gender = 1
        End If
        sSQL = "update member set email=@email,gender=@gender,phone=@phone,cellphone=@cellphone,active=@active where GAPortal_ID=@id"


        ps.addParameter("gender", gender)
        ps.addParameter("phone", txtPhone.Text)
        ps.addParameter("cellphone", txtCellphone.Text)
        ps.addParameter("active", act)
        ps.strSql = sSQL
        objfind.es(ps, strErr)
        If isHas.Text = "" Then
            sSQL = "insert into member_info(id,member_id,member_name,lang_flag)"
            sSQL = sSQL & " values (@uuid,@id,@userName,@languageFlag)"

            ps.addParameter("uuid", System.Guid.NewGuid().ToString())
            ps.addParameter("userName", txtName.Text)
            ps.addParameter("languageFlag", langFlag)
            ps.strSql = sSQL
            objfind.es(ps, strErr)
        Else
            If langFlag = "zh" Then
                sSQL = "update member_info set member_name=@name where member_id=@id and lang_flag='zh'"
            ElseIf langFlag = "en" Then
                sSQL = "update member_info set member_name=@name where member_id=@id and lang_flag='en'"
            End If

            ps.addParameter("name", txtName.Text)


            ps.strSql = sSQL
            objfind.es(ps, strErr)
        End If





        If strErr = Nothing Or strErr = "" Then
            email.Text = txtEmail.Text
            isHas.Text = txtName.Text
            result.InnerHtml = ""
            result.InnerHtml = "<center><b><font color=""#FF0000""> Update Success！</font></b></center>"
        Else
            result.InnerHtml = ""
            result.InnerHtml = "<center><b><font color=""#FF0000"">Error！</font></b><p>" & strErr & "</p></center>"
        End If

    End Sub

    Protected Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
        Response.Redirect("admfindmember.aspx")
    End Sub
End Class